<?php

/**
 * Users table mapper
 *
 */
class Application_Model_UserDb
{
    protected $_table_class;

    public function __construct()
    {
        $this->_table_class='Application_Model_DbTable_User';
    }
    
    /**
     * Returns an array with users objects
     *
     * @return array
     */
    public function fetchUsers()
    {
        $users=array();
        
        $user_tbl=new $this->_table_class();
        
        $user_rows=$user_tbl->fetchAll();
        
        foreach ($user_rows as $row)
        {
            $user=new Application_Model_User($row);
            
            $users[]=$user;
        }
        
        return $users;
    }
    
    /**
     * Return user object by ID
     *
     * @param int $id
     * @return object
     */
    public function fetchUserById($id)
    {
        $user_tbl=new $this->_table_class();
        
        $row=$user_tbl->fetchRow(array('id=?'=>$id));
        
        $user=new Application_Model_User($row);
        
        return $user;
    }
    
    /**
     * Return user object by login
     *
     * @param string $login
     * @return object
     */
    public function fetchUserByLogin($login)
    {
        $user_tbl=new $this->_table_class();
        
        $row=$user_tbl->fetchRow(array('login=?'=>$login));
        
        $user=new Application_Model_User($row);
        
        return $user;
    }
    
    /**
     * Return user object by email
     *
     * @param string $email
     * @return object
     */
    public function fetchUserByEmail($email)
    {
        $user_tbl=new $this->_table_class();
        
        $row=$user_tbl->fetchRow(array('email=?'=>$email));
        
        $user=new Application_Model_User($row);
        
        return $user;
    }
    
    /**
     * Saves user data to database
     *
     * @param mixed $user
     * 
     * @return int
     */
    public function save($user)
    {
        if (is_object($user) && ($user instanceof Application_Model_User))
        {
            $user=$user->toArray();
        }
        
        if (is_array($user))
        {
            $user_tbl=new $this->_table_class();
            
            if (!empty($user['id']))
            {
                return $user_tbl->update($user,array('id=?'=>$user['id']));
            } else 
            {
                return $user_tbl->insert($user);
            }
        }
    }
    
    /**
     * Deletes user from database
     *
     * @param mixed $user
     * @return int
     */
    public function delete($user)
    {
        if (is_object($user) && ($user instanceof Application_Model_User))
        {
            $user=$user->toArray();
        }
        
        if (is_array($user) && !empty($user['id']))
        {
            $user_tbl=new $this->_table_class();

            return $user_tbl->delete(array('id=?'=>$user['id']));
        }
    }
    
    /**
     * Returns users paginator
     *
     * @param int $page
     * @return array
     */
    public function getUsersPage($page=1,$limit=10)
    {
        $users=array();
        
        $db=Zend_Registry::get('db');
        
        $adapter = new Zend_Paginator_Adapter_DbSelect($db->select()->from(Application_Model_User::DB_TABLE));
        
        $rowCount = $db->select()->from(Application_Model_User::DB_TABLE)
            ->reset(Zend_Db_Select::COLUMNS)
            ->columns(new Zend_Db_Expr('COUNT(*) AS '.$db->quoteIdentifier(Zend_Paginator_Adapter_DbSelect::ROW_COUNT_COLUMN)));

        $adapter->setRowCount($rowCount);
        
        $paginator = new Zend_Paginator($adapter);
        $paginator->setCurrentPageNumber((int) $page);
        $paginator->setItemCountPerPage($limit);
        
        if (count($paginator))
        {
            foreach ($paginator as $row)
            {
                $user=new Application_Model_User($row);
            
                $users[]=$user;
            }
        }
        
        return array($users,$paginator);
    }
}

